//
//  File.swift
//  
//
//  Created by tangbl93 on 2021/2/19.
//

import Foundation

public extension Error {
    
    /// Return the NSUnderlyingError
    var yx_underlyingError: Error? {
        
        guard let underlyingError = (self as NSError).userInfo[NSUnderlyingErrorKey] as? NSError else {
            return nil
        }
        return underlyingError
    }
    
    /// Return the underlyingError recursion
    var yx_underlyingErrors: [Error] {
        
        var underlyingErrors:[Error] = []
        var underlyingError:Error? = self.yx_underlyingError
        
        repeat {
            if let underlyingError = underlyingError {
                underlyingErrors.append(underlyingError)
            }
            
            underlyingError = underlyingError?.yx_underlyingError
        } while underlyingError != nil
        
        return underlyingErrors
    }
}
